<?php
// +----------------------------------------------------------------------
// | 后台文章管理控制器
// +----------------------------------------------------------------------
// | Author: Eric <ieyangxiaoguo@126.com>
// +----------------------------------------------------------------------
// | Copyright ©2017 http://www.zzuyxg.top All rights reserved.
// +----------------------------------------------------------------------
// | 2017年10月12日
// +----------------------------------------------------------------------
namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\RequirementModel;
use app\common\model\RequirementTypeModel;
use app\common\model\FruitModel;
use app\common\model\FruitTypeModel;
use think\Db;

class AuditController extends AdminBaseController
{
    /**
     * 文章列表
     */
    public function index()
    {
        $typeModel = new RequirementTypeModel();

        $categories = $typeModel->getAll();

        $this->assign('categories', $categories);

        return $this->fetch('requirement');
    }

        public function requirement()
    {
        $typeModel = new RequirementTypeModel();

        $categories = $typeModel->getAll();

        $this->assign('categories', $categories);

        return $this->fetch();
    }


        public function requirementaudit()
    {
        $typeModel = new RequirementTypeModel();

        $categories = $typeModel->getAll();

        $this->assign('categories', $categories);

        return $this->fetch();
    }



    public function getRequirementList(){
        $map = [];
        $catID = $this->request->param('catID/d', 0, 'intval');
        $keyword = $this->request->param('keyword', '');

        // $map[] = ['post_type','=',0];

        if ($catID) {
            $map[] = ['type','=',$catID];
        }
        if ($keyword != '') {
            $map[] = ['title','like',$keyword.'%'];
        }

        $map[] = ['status','eq',0];

        $count = RequirementModel::where($map)->count();

        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');

        $lists = RequirementModel::where($map)->with(['requirement_type', "teacher", "member"])->order('id desc')->limit(($page-1)*$limit,$limit)->all();

        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $lists;
        $res['count'] = $count;
        return $res;
    }


    public function getRequirementListAudit(){
        $map = [];
        $status = $this->request->param('status/d', 0, 'intval');
        $keyword = $this->request->param('keyword', '');

        // $map[] = ['post_type','=',0];

        if ($status != 0) {
            $map[] = ['status','=',$status];
        }else{
            $map[] = ['status','neq',0];
        }
        if ($keyword != '') {
            $map[] = ['title','like',$keyword.'%'];
        }

        $count = RequirementModel::where($map)->count();

        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');

        $lists = RequirementModel::where($map)->with(['requirement_type', "teacher", "member"])->order('id desc')->limit(($page-1)*$limit,$limit)->all();

        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $lists;
        $res['count'] = $count;
        $res['sql'] = Db::getLastSql();
        return $res;
    }


    public function fruit()
    {
        $typeModel = new FruitTypeModel();

        $categories = $typeModel->getAll();

        $this->assign('categories', $categories);

        return $this->fetch();
    }


    public function fruitAudit()
    {
        $typeModel = new FruitTypeModel();

        $categories = $typeModel->getAll();

        $this->assign('categories', $categories);

        return $this->fetch();
    }


    public function getFruitList(){
        $map = [];
        $catID = $this->request->param('catID/d', 0, 'intval');
        $keyword = $this->request->param('keyword', '');

        // $map[] = ['post_type','=',0];

        if ($catID) {
            $map[] = ['type','=',$catID];
        }
        if ($keyword != '') {
            $map[] = ['title','like',$keyword.'%'];
        }
        $map[] = ['status','eq',0];
        $count = FruitModel::where($map)->count();

        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');

        $lists = FruitModel::where($map)->with(["fruit_type","teacher", "member"])->order('id desc')->limit(($page-1)*$limit,$limit)->all();

        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $lists;
        $res['count'] = $count;
        $res['sql'] = Db::getLastSql();
        $res['sql'] = Db::getLastSql();
        return $res;
    }
    public function getFruitListAudit(){
        $map = [];
        $status = $this->request->param('status/d', 0, 'intval');
        $keyword = $this->request->param('keyword', '');

        // $map[] = ['post_type','=',0];

        if ($status != 0) {
            $map[] = ['status','=',$status];
        }else{
            $map[] = ['status','neq',0];
        }
        if ($keyword != '') {
            $map[] = ['title','like',$keyword.'%'];
        }
        $count = FruitModel::where($map)->count();

        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');

        $lists = FruitModel::where($map)->with(["fruit_type","teacher", "member"])->order('id desc')->limit(($page-1)*$limit,$limit)->all();

        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $lists;
        $res['count'] = $count;
        $res['sql'] = Db::getLastSql();
        $res['sql'] = Db::getLastSql();
        return $res;
    }


    public function accept(){
        $param = $this->request->param();
        $type = $param['type'];
        $id = $param['id'];
        switch ($type){
            case 3:
                Db::name('requirement')->where('id',$id)->update([
                    'status'=> 1,
                    'audit_time' => date("Y-m-d H:i:s",time())
                ]);
                addLog("审核通过了需求ID:".$id,Db::getLastSql());

                break;
            case 4:
                Db::name('fruit')->where('id',$id)->update([
                    'status'=> 1,
                    'audit_time' => date("Y-m-d H:i:s",time())
                ]);
                addLog("审核通过了成果ID:".$id,Db::getLastSql());
                break;
            default:
                break;
        }
        return [
            'code' => 0,
            'msg' => '审核成功'
        ];
    }
    public function reject(){
        $param = $this->request->param();
        $type = $param['type'];
        $id = $param['id'];
        switch ($type){
            case 3:
                Db::name('requirement')->where('id',$id)->update([
                    'status'=> 2,
                    'audit_time' => date("Y-m-d H:i:s",time())
                ]);
                addLog("拒绝了需求ID:".$id,Db::getLastSql());
                break;
            case 4:
                Db::name('fruit')->where('id',$id)->update([
                    'status'=> 2,
                    'audit_time' => date("Y-m-d H:i:s",time())
                ]);
                addLog("拒绝了成果ID:".$id,Db::getLastSql());
                break;
            default:
                break;
        }

        return [
            'code' => 0,
            'msg' => '审核成功'
        ];
    }
}
